package com.cdd.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.goods.mapper.GoodsTypeMapper;
import com.cdd.goods.service.IGoodsTypeService;
import com.common.entity.GoodsStorage;
import com.common.entity.GoodsType;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-01
 */
@Service
public class GoodsTypeServiceImpl implements IGoodsTypeService {
@Autowired
private GoodsTypeMapper goodsTypeMapper;

    @Override
    public CommonResult selectAll(Integer currentPage,Integer pageSize,GoodsType goodsType) {
        System.out.println("++"+goodsType);
        Page<GoodsType> page=new Page<>(currentPage,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(goodsType.getTypename())){
            wrapper.like("typename",goodsType.getTypename());
        }
        if (goodsType.getIsDeleted()!=null){
            wrapper.eq("is_deleted",goodsType.getIsDeleted());
        }
        Page page1 = goodsTypeMapper.selectPage(page, wrapper);
        return new CommonResult(2000,"查询成功",page1);
    }

    @Override
    public CommonResult delectGoodsType(Integer id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        GoodsType goodsType=new GoodsType();
        goodsType.setIsDeleted(1);
        goodsTypeMapper.update(goodsType,wrapper);
        return new CommonResult(2000, "删除成功", null);
    }

    @Override
    public CommonResult updateGoodsType(GoodsType goodsType) {
        GoodsType goodsType1=new GoodsType();
        goodsType1.setId(goodsType.getId());
        goodsType1.setTypename(goodsType.getTypename());
        goodsTypeMapper.updateById(goodsType1);
        return new CommonResult(2000, "修改成功", null);
    }

    @Override
    public CommonResult insertType(@RequestBody GoodsType goodsType) {
        goodsType.setIsDeleted(0);
        List<GoodsType> goodsBrands = goodsTypeMapper.selectTypename();
        for (GoodsType g:goodsBrands) {
            if (g.getTypename().equals(goodsType.getTypename())){
                return new CommonResult(5000, "添加失败，已有该商品类别", null);
            }

        }
        goodsTypeMapper.insert(goodsType);
        return new CommonResult(2000, "添加成功", null);
    }

    @Override
    public CommonResult select() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        List list = goodsTypeMapper.selectList(wrapper);
        return new CommonResult(2000,"查询成功",list);
    }
}
